﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.Common.Entity;

namespace ESOL.WinUI.Hr
{
    public partial class frmMaintainPosition : BaseForm.frmBaseDialogyMaintainForm
    {
        #region Variable
        public MstPositionEntity SelectedPosition { get; set; }
        #endregion

        #region Constructor
        public frmMaintainPosition()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void SetFields()
        {
            if (SelectedPosition == null)
            {
                return;
            }
            txtPositionCode.Text = SelectedPosition.PositionCode;
            txtPositionName.Text = SelectedPosition.PositionName;
            cmbDeleteFlag.SetSelectedValue(SelectedPosition.DeleteFlag);
            cmbPositionLevel.SetSelectedValue(SelectedPosition.Level);
            txtDescription.Text = SelectedPosition.Description;
        }

        private void SetEntity()
        {
            if (SelectedPosition == null)
            {
                SelectedPosition = new MstPositionEntity();
                SelectedPosition.PositionId = Guid.NewGuid().ToString();
            }
            SelectedPosition.PositionCode = txtPositionCode.Text;
            SelectedPosition.PositionName = txtPositionName.Text;
            SelectedPosition.DeleteFlag = CommonFunctions.ToInt32(cmbDeleteFlag.SelectedValue);
            if (cmbPositionLevel.SelectedValue != null)
            {
                SelectedPosition.Level = CommonFunctions.ToString(cmbPositionLevel.SelectedValue);
            }
            SelectedPosition.Description = txtDescription.Text;
        }

        private bool CheckData()
        {
            if (string.IsNullOrEmpty(txtPositionCode.Text))
            {
                MessageBox.Show("职位代码不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtPositionName.Text))
            {
                MessageBox.Show("职位名称不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }

        private bool Save()
        {
            try
            {
                if (CheckData() == false)
                {
                    return false;
                }
                SetEntity();
                if (ThisActionType == ActionType.New)
                {
                    bool isSuccessful = MstPositionBs.InsertMstPosition(SelectedPosition, CommonParameter.CurrentUser.PersonId);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("新增失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Modify)
                {
                    bool isSuccessful = MstPositionBs.UpdateMstPosition(SelectedPosition);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("修改失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        #endregion

        #region Event
        private void frmMaintianPosition_Load(object sender, EventArgs e)
        {
            UICommon.UICommonFunctions.BindComboboxByEnum(cmbDeleteFlag, typeof(DeleteFlag), typeof(int), false);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbPositionLevel, "PLEVEL", false);
            SetFields();
        }
        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }
        #endregion
    }
}
